package com.avic.map.controller;

import com.avic.map.common.constant.LocalResourceConstant;
import com.avic.map.common.constant.SystemLogConstant;
import com.avic.map.common.constant.UserConstant;
import com.avic.map.common.convertor.SingleSoldierResourceConvertor;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.lang.BaseResponse;
import com.avic.map.common.util.lang.ServiceCallBack;
import com.avic.map.controller.request.CreateSingleSoldierResourceReq;
import com.avic.map.controller.request.GetSingleSoldierResourceListReq;
import com.avic.map.controller.request.UpdateSingleSoldierResourceReq;
import com.avic.map.controller.result.BaseCreateResourceRes;
import com.avic.map.controller.result.GetSingleSoldierListRes;
import com.avic.map.controller.result.GetSoldierResourceRes;
import com.avic.map.dal.condition.PageQuerySingleSoldierCondition;
import com.avic.map.dal.model.SingleSoldierResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("resource/singlesoldier")
@Api(tags = "单兵资源", description = "分页查询单兵资源列表&创建单兵资源&修改单兵资源&删除单兵资源")
public class SingleSoldierResourceController extends AbstractFacadeImpl {

    @ApiOperation(value = "分页查询单兵资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "getSingleSoldierResourceListReq", required = true, dataType = "GetSingleSoldierResourceListReq")
    })
    @PostMapping("list")
    public GetSingleSoldierListRes getSingleSoldierListReq(@RequestBody GetSingleSoldierResourceListReq getSingleSoldierResourceListReq) {
        final GetSingleSoldierListRes result = new GetSingleSoldierListRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                PageQuerySingleSoldierCondition pageQuerySingleSoldierCondition = new PageQuerySingleSoldierCondition(
                        getSingleSoldierResourceListReq.getCurrentPage(), getSingleSoldierResourceListReq.getPageSize()
                );
                pageQuerySingleSoldierCondition.setPersonnelNumber(getSingleSoldierResourceListReq.getPersonnelNumber());
                pageQuerySingleSoldierCondition.setPersonnelCompany(getSingleSoldierResourceListReq.getPersonnelCompany());
                pageQuerySingleSoldierCondition.setEquipmentType(getSingleSoldierResourceListReq.getEquipmentType());

                List<SingleSoldierResource> singleSoldiers = singleSoldierResourceService.itemList(pageQuerySingleSoldierCondition);
                result.setItemList(SingleSoldierResourceConvertor.SingleSoldierListToSingleSoldierListInfoList(singleSoldiers));
                result.setItemTotal(singleSoldierResourceService.queryRecordsTotal(pageQuerySingleSoldierCondition));
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "创建单兵资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createSingleSoldierResourceReq", required = true, dataType = "CreateSingleSoldierResourceReq")
    })
    @PostMapping
    public BaseCreateResourceRes createSingleSoldier(@RequestBody CreateSingleSoldierResourceReq createSingleSoldierResourceReq,
                                                     @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseCreateResourceRes result = new BaseCreateResourceRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isNotBlank(createSingleSoldierResourceReq.getPersonnelNumber(), "单兵编号不能为空");
                AssertUtil.isTrue(createSingleSoldierResourceReq.getEquipmentType() == 1 ||
                        createSingleSoldierResourceReq.getEquipmentType() == 2, BizErrorCode.PARAM_ERROR, "单兵类型不在取值范围");

            }

            @Override
            public void doService() throws Exception {

                SingleSoldierResource singleSoldier = new SingleSoldierResource();
                singleSoldier.setPersonnelCompany(createSingleSoldierResourceReq.getPersonnelCompany());
                singleSoldier.setPersonnelNumber(createSingleSoldierResourceReq.getPersonnelNumber());
                singleSoldier.setCallSign(createSingleSoldierResourceReq.getCallSign());
                singleSoldier.setEquipmentType(createSingleSoldierResourceReq.getEquipmentType());
                if (createSingleSoldierResourceReq.getEquipmentType() == 2) {
                    singleSoldier.setVideoAddress(StringUtils.EMPTY);
                } else {
                    singleSoldier.setVideoAddress(createSingleSoldierResourceReq.getVideoAddress());
                }

                result.setItemId(String.valueOf(singleSoldierResourceService.create(singleSoldier)));

                LocalResourceConstant.SOLDIER_STATE_DB_MAP.put(singleSoldier.getPersonnelNumber(), singleSoldier);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.CREATE_SOLDIER_RESOURCE, result.getItemId(),
                                objectMapper.writeValueAsString(createSingleSoldierResourceReq)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "修改单兵资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateSingleSoldierResourceReq", required = true, dataType = "UpdateSingleSoldierResourceReq")
    })
    @PutMapping
    public BaseResponse updateSingleSoldier(@RequestBody UpdateSingleSoldierResourceReq updateSingleSoldierResourceReq,
                                            @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(updateSingleSoldierResourceReq.getItemId() > 0, BizErrorCode.PARAM_ERROR, "itemId is blank");
                AssertUtil.isNotBlank(updateSingleSoldierResourceReq.getPersonnelNumber(), "单兵编号不能为空");
                AssertUtil.isTrue(updateSingleSoldierResourceReq.getEquipmentType() == 1 ||
                        updateSingleSoldierResourceReq.getEquipmentType() == 2, BizErrorCode.PARAM_ERROR, "单兵类型不在取值范围");

            }

            @Override
            public void doService() throws Exception {

                SingleSoldierResource singleSoldier = new SingleSoldierResource();
                singleSoldier.setPersonnelCompany(updateSingleSoldierResourceReq.getPersonnelCompany());
                singleSoldier.setPersonnelNumber(updateSingleSoldierResourceReq.getPersonnelNumber());
                singleSoldier.setCallSign(updateSingleSoldierResourceReq.getCallSign());
                singleSoldier.setEquipmentType(updateSingleSoldierResourceReq.getEquipmentType());
                singleSoldier.setId(updateSingleSoldierResourceReq.getItemId());

                if (updateSingleSoldierResourceReq.getEquipmentType() == 2) {
                    singleSoldier.setVideoAddress(StringUtils.EMPTY);
                } else {
                    singleSoldier.setVideoAddress(updateSingleSoldierResourceReq.getVideoAddress());
                }

                singleSoldierResourceService.update(singleSoldier);

                LocalResourceConstant.SOLDIER_STATE_DB_MAP.put(singleSoldier.getPersonnelNumber(), singleSoldier);
                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.UPDATE_SOLDIER_RESOURCE, updateSingleSoldierResourceReq.getItemId(),
                                objectMapper.writeValueAsString(updateSingleSoldierResourceReq)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "删除单兵资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", paramType = "path", value = "单兵资源ID", required = true)
    })
    @DeleteMapping("/{itemId}")
    public BaseResponse deleteSingleSoldier(@PathVariable("itemId") int itemId,
                                            @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(itemId > 0, BizErrorCode.PARAM_ERROR, "数据ID大于零");

            }

            @Override
            public void doService() throws Exception {
                singleSoldierResourceService.delete(itemId);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.DELETE_SOLDIER_RESOURCE, itemId), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "获取的单兵详情数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "soldierNumber", paramType = "path", value = "单兵资源编号", required = true)
    })
    @GetMapping("/{itemId}")
    public GetSoldierResourceRes getSoldierResource(@PathVariable("soldierNumber") String soldierNumber) {
        final GetSoldierResourceRes result = new GetSoldierResourceRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isNotBlank(soldierNumber, "编号为空");

            }

            @Override
            public void doService() throws Exception {
                SingleSoldierResource singleSoldierResource = singleSoldierResourceService.getSingleSoldierResourceByNumber(soldierNumber);
                AssertUtil.isTrue(null != singleSoldierResource, BizErrorCode.PARAM_ERROR, "基础资源里面未未配置该单兵的信息");
                result.setCallSign(singleSoldierResource.getCallSign());
                result.setPersonnelCompany(singleSoldierResource.getPersonnelCompany());
                result.setPersonnelNumber(singleSoldierResource.getPersonnelNumber());
                result.setVideoAddress(singleSoldierResource.getVideoAddress());
                result.setSuccess(true);
            }
        });
    }
}
